package com.thinkgem.jeesite.common.utils;

import org.apache.commons.beanutils.Converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author lucas
 */
public class DateConvert implements Converter {

    public Object convert(Class arg0, Object arg1) {
        if (arg1 == null)
            return null;
        String p = arg1.toString();
        if (p == null || p.trim().length() == 0) {
            return null;
        }
        try {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return df.parse(p.trim());
        } catch (Exception e) {
            return formatDate(p, arg1);
        }
    }

    public Object formatDate(String p, Object arg1) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        try {
            return df.parse(p.trim());
        } catch (ParseException e) {
            return arg1;
        }
    }

}